package com.thomas.framework.service.impl;

import com.thomas.framework.core.AppException;
import com.thomas.framework.core.IService;
import com.thomas.framework.core.ServiceBase;
import com.thomas.framework.dao.ISysModelDao;
import com.thomas.framework.entity.SysModel;
import com.thomas.framework.entity.SysRole;
import com.thomas.framework.entity.SysUser;
import com.thomas.framework.service.ISysModelService;
import com.thomas.framework.utils.CommonUtil;
import com.thomas.framework.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by taohj on 2014/4/23.
 */
@Service
public class SysModelServiceImpl extends ServiceBase<SysModel> implements ISysModelService {

    private ISysModelDao dao;


    @Autowired
    public void setDao(ISysModelDao dao) {
        this.dao = dao;
    }


    @Override
    public boolean create(SysModel model) {

        if (model!=null && model.getParent()!=null){
            SysModel parent=this.getById(model.getParent());
            if (parent!=null) {
                parent.setIsLeaf("0");
                if (!this.modify(parent)) {
                    throw new AppException("modify parent Failure");
                }
            }
        }
        model.setId(CommonUtil.getUUID());
        return dao.insert(model) > 0;
    }

    @Override
    public boolean remove(SysModel model) {

        List<SysModel> models=this.getChrildren(model);
        if (models!=null && models.size()>0){
            throw  new AppException("That there are sub-menus, please delete the sub-menu.");
        }
        int result = dao.delete(model);
        return result > 0;
    }

    @Override
    public boolean modify(SysModel model) {
        return dao.update(model) > 0;
    }

    @Override
    public SysModel getById(SysModel model) {
        return dao.selectById(model);
    }

    @Override
    public List<SysModel> getList(SysModel model) {
        return dao.selectList(model);
    }


    public List<SysModel> getFunctionMenuBySysRole(SysRole role) {
        return dao.seleFunctionMenuBySysRole(role);
    }


    public  List<SysModel> getMenuByAdmin(){
        return dao.selectMenuByAdmin();
    }





    public  List<SysModel> getFunctionByAdmin(){
        return  dao.selectFunctionByAdmin();
    }



    public List<SysModel> getFunctionByUser(SysUser user) {
        return dao.selectFunctionByUser(user);
    }


    @Override
    public PageResult<SysModel> getByPage(PageResult<SysModel> pageResult) {
        return pageResult.setRows(dao.selectbyPage(pageResult));
    }

    @Override
    public SysModel get(SysModel model) {
        return dao.select(model);
    }


    public List<SysModel> getTreeModel(SysUser user) {

        return  getTreeModel(user.get_functionMenus());
    }

    public  List<SysModel> getTreeModel(SysModel model){
        List<SysModel> allModels=dao.selectList(model);
        return  getTreeModel(allModels);
    }


    public List<SysModel> getTreeModel(List<SysModel> allModels){
        List<SysModel> rootModels = new ArrayList<SysModel>();
        for (int i = 0; i < allModels.size(); i++) {
            if (allModels.get(i).getParent() == null || !StringUtils.hasLength(allModels.get(i).getParent().getId())) {
                if (!rootModels.contains(allModels.get(i))) {
                    rootModels.add(allModels.get(i));
                }
                //allModels.remove(i);
                getTreeChidren(allModels, allModels.get(i));
            }
        }
        return rootModels;

    }



    public SysModel getTreeChidren(List<SysModel> models, SysModel model) {

        model.setChildren(model.getChildren() == null ? new ArrayList<SysModel>() : model.getChildren());
        for (int i = 0; i < models.size(); i++) {

            SysModel m = models.get(i);
            if (m.getParent() != null && model.getId().equals(m.getParent().getId())) {
                if (!model.getChildren().contains(m)) {
                    model.getChildren().add(m);
                }
                //models.remove(m);
                getTreeChidren(models, m);
            }
        }

        return model;
    }


    public List<SysModel> getChrildren(SysModel model) {
        SysModel searchModel=new SysModel();
        searchModel.setParent(model);

        List<SysModel> list=dao.selectList(searchModel);
        for (int i=0;i<list.size();i++){
            SysModel m=list.get(i);
            if ("0".equals(m.getIsLeaf())){
                m.setState("closed");
            }
        }

        return list;
    }



    public List<SysModel> getMenuByUser(SysUser user){
        return  dao.selectMenuByUser(user);
    }

    public List<SysModel> getTreeModel(SysRole role) {

        List<SysModel> roleSysModels=dao.seleFunctionMenuBySysRole(role);

        List<SysModel> allSysModels=this.getList(null);
        for (SysModel roleModel : roleSysModels) {
            for (SysModel model : allSysModels) {
                if (model.getId().equals(roleModel.getId()) && "1".equals(model.getIsLeaf())) {
                    model.setChecked(true);
                    break;
                }
            }
        }
        return   getTreeModel(allSysModels);
    }


}
